package com.andnnl.mongo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;


public class DateUtil {
    private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

    public static void main(String[] agrs) throws ParseException {

        String dd = "2016-07-29 09:21:38";
        logger.info(formatDateStr(dd, "yyyyMMddHHmmss"));
		
		 /*
		Date date;
		date =new Date();// stringToDate("2016-07-14 18:39:50",fmt);
		 
		//date=new Date();
		String dateString = "Fri Feb 01 00:00:00 GMT+08:00 2013";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'",Locale.CHINESE);
		TimeZone tz = TimeZone.getTimeZone("GMT+8");
		sdf.setTimeZone(tz);
		 
		String str = sdf.format(date);
		System.out.println("1===>"+str);
		//System.out.println("BsonDate===>"+StringToBsonDate("sndTime",dateToString(date,fmt)));

        
		 
	       Calendar cal = Calendar.getInstance();
	        cal.getTimeInMillis();
	        //北京时区GMT+8
	        Calendar beijingcal = Calendar.getInstance();
	        beijingcal.clear();
	        beijingcal.setTimeZone(TimeZone.getTimeZone("GMT+8"));
	        beijingcal.setTimeInMillis(cal.getTimeInMillis());
	        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
	        String beijingFormatStr = fmt.format(beijingcal.getTime());
	        System.out.println("==>"+beijingFormatStr);  
	        
	        */


    }

    public static String fmt = "yyyy-MM-dd HH:mm:ss";


    public static String dateToUTC(Date date) {
        TimeZone tz1 = TimeZone.getTimeZone("UTC");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); // Quoted "Z" to indicate UTC, no timezone offset
        df.setTimeZone(tz1);
        String nowAsISO = df.format(date);
        logger.info("nowAsISO=" + nowAsISO);
        return nowAsISO;
    }


    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

    public static String formatDateStr(String str, String dateFormat) {
        Date d;
        try {
            d = stringToDate(str, fmt);
            return dateToString(d, dateFormat);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";

    }


    public static String transferLongToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = new Date(millSec);
        return sdf.format(date);
    }

    public static long transferDateToLong(String dateFormat, String date) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        long l = 1L;
        try {
            l = sdf.parse(date).getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return l;
    }

    public static Date transferLongToDate(Long millSec) {
        Date date = new Date(millSec);
        return date;
    }

    // date类型转换为String类型
    // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    // data Date类型的时间
    public static String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }

    // long类型转换为String类型
    // currentTime要转换的long类型的时间
    // formatType要转换的string类型的时间格式
    public static String longToString(long currentTime, String formatType) throws ParseException {
        Date date = longToDate(currentTime, formatType); // long类型转成Date类型
        String strTime = dateToString(date, formatType); // date类型转成String
        return strTime;
    }

    // string类型转换为date类型
    // strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒，
    // strTime的时间格式必须要与formatType的时间格式相同
    public static Date stringToDate(String strTime, String formatType) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }

    // long转换为Date类型
    // currentTime要转换的long类型的时间
    // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    public static Date longToDate(long currentTime, String formatType) throws ParseException {
        Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
        String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
        Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
        return date;
    }

    // string类型转换为long类型
    // strTime要转换的String类型的时间
    // formatType时间格式
    // strTime的时间格式和formatType的时间格式必须相同
    public static long stringToLong(String strTime, String formatType) throws ParseException {
        Date date = stringToDate(strTime, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date类型转成long类型
            return currentTime;
        }
    }

    // date类型转换为long类型
    // date要转换的date类型的时间
    public static long dateToLong(Date date) {
        return date.getTime();
    }

}
